Configuring your database
You need to extend the indexed-db-configuration service:
import IndexedDbConfigurationService from 'ember-indexeddb/services/indexed-db-configuration';
export default class ExtendedIndexedDbConfigurationService extends IndexedDbConfigurationService {
currentVersion = 1;
version1: {
stores: {
'model-one': '&id,*isNew',
'model-two': '&id,*status,*modelOne,[status+modelOne]'
}
}
}
Please consult the Dexie Documentation on details about configuring your database.
Basically, you should always have an &id
column as a primary index, and add further indices that you want to query by. You can also add multi-indices for more complex querying.
Each version can also have an upgrade function in addition (or instead of) a stores property. This can be used to do data migrations. See the Dexie Documentation for details about data migrations and upgrades.
You can add as many version as you want, and Dexie will handle the upgrading for you. Note that you cannot downgrade a version. There needs to be a versionX
property per version, starting at 1. So if you have a currentVersion
of 3, you need to have version1
, version2
and version3
properties.
You do not need to keep old versionX configurations unless they contain an upgrade.
All of these migrations are automatically run when running this.indexedDb.setup();
.
In addition to the store configuration, you also need to define a mapTable
.
This is a map of functions which is used to normalize JSONAPI payloads for IndexedDB.
For the above example, this should look something like this:
mapTable: {
'model-one': (item) => {
return {
id: this._toString(item.id),
json: this._cleanupObject(item),
isNew: this._toZeroOne(item.isNew)
};
},
'model-two': (item) => {
return {
id: this._toString(item.id),
json: this._cleanupObject(item),
modelOne: this._toString(item.relationships.modelOne?.data?.id),
status: item.attribtues.status
};
}
}
Things to note here:
- Always convert your IDs to strings. You can use the provided
this._toString(val)
function for this. - Always clean up your json. You can use the provided
this._cleanObject(item)
for this, which will clean up prototype & meta properties/functions. - IndexedDB doesn't work with boolean queries. You need to convert booleans to 1/0 when inserting it into the Database. You can use the provided
this._toZeroOne(val)
for this.
You should have one property per item you want to query for. The item which is passed in is a JSONAPI payload-item.